/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.base.fs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.hsqldb.lib.FileAccess;
import org.webos.java.base.Session;
import org.webos.java.root.Log;
import org.webos.java.base.SessionManager;

/**
 *
 * @author Kostya
 */
public class DBFileAccess implements FileAccess{

    static Session getSession(String aFileName)throws IOException{
        int pos = aFileName.indexOf("/");
        if(pos==-1)
            throw new IOException("Invalid session");
        String sessionID = aFileName.substring(0, pos);
        Session sess = SessionManager.getSessionByID(sessionID);
        if(sess==null)
            throw new IOException("Can't find session "+sessionID);
        return sess;
    }

    static String getFileName(String aFileName)throws IOException{
        int pos = aFileName.indexOf("/");
        if(pos==-1)
            throw new IOException("Invalid file name provided");
        return aFileName.substring(pos+1);
    }

    public DBFileAccess(Object aData) {
        Log.info(this, "DBFileAccess created, properties passed: "+aData);
    }

    public InputStream openInputStreamElement(String arg0) throws IOException {
        try {
//            Log.debug(this, "openInputStreamElement: "+arg0, getFileName(arg0));
            FileDescriptor fd = getSession(arg0).getRootProvider().createPathToFileDescriptor(getFileName(arg0));
            if(fd!=null)
                return fd.getProvider().getInputStream(fd);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        throw new IOException("Can't open stream for "+arg0);
    }

    public OutputStream openOutputStreamElement(String arg0) throws IOException {
//        Log.debug(this, "openOutputStreamElement: "+arg0);
        try {
            FileDescriptor fd = getSession(arg0).getRootProvider().createPathToFileDescriptor(getFileName(arg0));
            if(fd!=null){
                fd.setType("text/plain");
                if(fd.getName().indexOf(".backup")!=-1)
                    fd.setType("text/unknown");
                return fd.getProvider().getOutputStream(fd);
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        throw new IOException("Can't open stream for "+arg0);
    }

    public boolean isStreamElement(String arg0) {
        boolean result = false;
        try {
            FileDescriptor fd = getSession(arg0).getRootProvider().
                    getFileDescriptor(getFileName(arg0));
            if(fd!=null)
                result = true;
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
//        Log.debug(this, "isStreamElement: "+arg0+" = "+result);
        return result;
    }

    public void createParentDirs(String arg0) {
//        Log.debug(this, "createParentDirs: "+arg0);
        try {
            FileDescriptor fd = getSession(arg0).getRootProvider().
                    createPathToFileDescriptor(getFileName(arg0));
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
    }

    public void removeElement(String arg0) {
//        Log.debug(this, "removeElement: "+arg0);
        try {
            FileDescriptor fd = getSession(arg0).getRootProvider().
                    getFileDescriptor(getFileName(arg0));
            if(fd!=null)
                fd.getProvider().removeFile(fd, false);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
    }

    public void renameElement(String arg0, String arg1) {
//        Log.debug(this, "renameElement: "+arg0+" to "+arg1);
        try {
            FileDescriptor fd = getSession(arg0).getRootProvider().getFileDescriptor(getFileName(arg0));
            if(fd!=null){
                String fileName = arg1;
                if(arg1.indexOf("/")!=-1){
                    fileName = fileName.substring(arg1.lastIndexOf("/")+1);
                }
//                Log.debug(this, "Renaming  "+fd.getName()+":"+fd.getId()+" to "+fileName);
                fd.setName(fileName);
                fd.getProvider().renameFile(fd);
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
    }

    public FileSync getFileSync(OutputStream arg0) throws IOException {
//        Log.debug(this, "getFileSync");
        return new DBFileSync(arg0);
    }

}
